package com.zhkc.wisdom.common.pojo.vo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Description: 分页响应对象
 * User: zzw
 * Date: 2023-08-17
 * Time: 10:42
 * company: zhkc
 */
@Schema(description = "分页响应对象")
@Data
@Builder
public class PageVO<T> implements Serializable {
    /**
     * 当前页码
     */
    @Schema(description = "当前页码")
    private Integer page;
    /**
     * 每页记录数
     */
    @Schema(description = "每页记录数")
    private Integer size;
    /**
     * 总记录数
     */
    @Schema(description = "总记录数")
    private Integer total;
    /**
     * 数据集合
     */
    @Schema(description = "数据集合")
    private List<T> records;

    public PageVO() {
    }

    /**
     * @Description TODO 统一返回分页信息
     * @Author zzw
     * @Date 15:40 2023/9/21
     **/
    public PageVO(long page, long size, long total, List<T> records) {
        //前端需要Integer类型
        this.page = Integer.parseInt(String.valueOf(page));
        this.size = Integer.parseInt(String.valueOf(size));
        this.total = Integer.parseInt(String.valueOf(total));
        this.records = records;
    }

    /**
     * @Description TODO 统一返回分页信息
     * @Author zzw
     * @Date 15:40 2023/9/21
     **/
    public PageVO(Page page, List<T> records) {
        if (page != null) {
            this.page = Integer.parseInt(String.valueOf(page.getCurrent()));
            this.size = Integer.parseInt(String.valueOf(page.getSize()));
            this.total = Integer.parseInt(String.valueOf(page.getTotal()));
        }
        this.records = records;
    }

    /**
     * @Description TODO 统一返回分页信息并将Bean转为指定VO
     * @Author lkh
     * @Date 15:40 2023/9/21
     **/
    public PageVO(IPage source, Class<T> clz) {
        List<T> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(source.getRecords())) {
            list = JSONUtil.toList(JSONUtil.parseArray(source.getRecords()), clz);
        }

        this.records = list;
        this.page = Integer.parseInt(String.valueOf(source.getPages()));
        this.size = Integer.parseInt(String.valueOf(source.getSize()));
        this.total = Integer.parseInt(String.valueOf(source.getTotal()));
    }
}
